<?php
require('jsonencode.php');
require('OModel.php');

$cmd = $_GET["cmd"];
$result;

if ($cmd == "Insert")
{
    $result = Insert($_GET['Datepick'], $_GET['plane'], $_GET['NNumber'], $_GET['From'],
                $_GET['To'], $_GET['Comment'], $_GET['Instr'], $_GET['Ldg'],
                $_GET['cmplx'],$_GET['tsel'],$_GET['tmel'],$_GET['tday'],$_GET['tnight'],$_GET['isim'],$_GET['iactual'],
                $_GET['ground'],$_GET['tpic'],$_GET['tsic'],$_GET['solodual']);
    echo $result;
}
if ($cmd == "Update"){
    echo "Update";
}

if ($cmd == "category"){
    echo LoadCategoryOptions();
}

if ($cmd == "InsertNNumber"){
    echo InsertNNumber($_GET['makeid'], $_GET['nnumber'], $_GET['comment']);
}

if ($cmd == "NNumberExists"){
    echo NNumberExists($_GET['nnumber']);
}


/*================================================
 * Insert record into Logbook
 */
function Insert($flightdate, $planeid, $nnumberid, $from, $to, $comment, $instr, $ldg,
         $cmplx, $tsel, $tmel, $tday, $tnight, $isim, $iactual, $ground, $tpic, $tsic, $solodual){
    $db_selected = mysql_select_db("calcwb",DBOpen());
    $date = $flightdate;
    $newdate = substr($date,7,4) . "-" . substr($date,1,2) . "-".substr($date,4,2);
    $AircraftID = GetAircraftID($planeid, $nnumberid);
    $sql = "INSERT INTO logbook (FlightDate, AircraftID, FromID, ToID, Comment, InstrumentApproach, Landings) VALUES (";
    $sql = $sql.'\''.$newdate.'\',\''.$AircraftID.'\', \''.$from.'\', \''.$to.'\', '.$comment.', \''.$instr.'\', \''.$ldg.'\')';
    $result = mysql_query($sql) or die(mysql_error());
    $sql = "SELECT @@IDENTITY as 'Identity'";
    $result = mysql_query($sql) or die(mysql_error());
    $row = mysql_fetch_array($result, MYSQL_ASSOC);
    $LogID = $row['Identity'];
    $result = mysql_query($sql) or die(mysql_error());
    $iRes = InsertDetails($LogID, $cmplx, $tsel, $tmel, $tday, $tnight, $isim, $iactual, $ground, $tpic, $tsic, $solodual);
    return $iRes;
}
/*================================================
 * Insert Logbook details
 */
function InsertDetails($LogID, $cmplx, $tsel, $tmel, $tday, $tnight, $isim, $iactual, $ground, $tpic, $tsic, $solodual){
    $db_selected = mysql_select_db("calcwb",DBOpen());
    $sql = "INSERT INTO logbookdetails (LogID, Complex, TSel, TMel, TDay, TNight, ISim, IActual, Ground, TPic,
            TSic, Solo) VALUES (";
    $sql = $sql.$LogID.', '.$cmplx.', '.$tsel.', '.$tmel.', '.$tday.', '.$tnight.', '.$isim.', '.$iactual.', ';
    $sql = $sql.$ground.', '.$tpic.', '.$tsic.', '.$solodual.')';
    $result = mysql_query($sql) or die(mysql_error());
    return $result;
}

/*================================================
 * Check if nnumber already in database
 */
function NNumberExists($nnumber){
    $db_selected = mysql_select_db("calcwb",DBOpen());
    $sql = "SELECT NNumber FROM aircraft WHERE NNumber=$nnumber";
    $result = mysql_query($sql) or die(mysql_error());
    $row = mysql_fetch_array($result, MYSQL_ASSOC);
    return $row['NNumber'];
}

/*================================================
 * Insert new N Number
 */
function InsertNNumber($makeid, $nnumber, $comment){
    $link = DBOpen();
    $db_selected = mysql_select_db("calcwb",$link);
    $sql = "INSERT INTO aircraft (MakeID, NNumber, Comment) VALUES ($makeid, $nnumber, $comment )";
    $result = mysql_query($sql) or die(mysql_error());
    mysql_close($link);
    return $result;
}

/*================================================
 * Get Aircraft ID
 */
function GetAircraftID($planeid, $nnumberid){
    $db_selected = mysql_select_db("calcwb",DBOpen());
    $sql = "SELECT AircraftID FROM aircraft ";
    $sql = $sql ."INNER JOIN make ON make.MakeID=aircraft.MakeID ";
    $sql = $sql ."WHERE aircraft.MakeID=".$planeid." AND NNumber='".$nnumberid."' ";
    $result = mysql_query($sql) or die(mysql_error());
    $row = mysql_fetch_array($result, MYSQL_ASSOC);
    return $row['AircraftID'];
}

/*================================================
 * get categories list
 */
function LoadCategoryOptions(){
    $link = DBOpen();
    $db_selected = mysql_select_db("calcwb",$link);
    $sql = "SELECT CategoryID AS value, Name AS text FROM category";
    $result = mysql_query($sql) or die(mysql_error());
    $result_total = mysql_num_rows($result)-1;
    $json = "["; 
    $i=0;
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
            if ( $i < $result_total) {
                    $json = $json.json_encode($row).",";
            }
            else {
                    $json = $json.json_encode($row);
            }
            $i = $i + 1;
    }
    $json = $json."]";
    mysql_close($link);
    return ($json);
}
?>
